package com.quwan.common.exception;

import com.quwan.common.Check;
import lombok.Data;

@Data
public class ResponseException extends RuntimeException {
    private Integer code;
    private String message;
    private String position;

    public ResponseException(Integer code, String message) {
        int index = 2;
        this.code = code;
        this.message = message;
        StackTraceElement[] ss = Thread.currentThread().getStackTrace();
        if (ss.length > 2) {
            StackTraceElement stack = ss[index];
            if (stack != null)
                if (stack.getClassName().equalsIgnoreCase(Check.class.getName()))
                    stack = ss[index + 1];
            this.position = new StringBuilder()
                    .append(stack.getClassName())
                    .append(".")
                    .append(stack.getMethodName())
                    .append("(")
                    .append(stack.getClassName().substring(stack.getClassName().lastIndexOf(".") + 1))
                    .append(".java")
                    .append(":")
                    .append(stack.getLineNumber())
                    .append(")")
                    .toString();
        }

    }


    @Override
    public String toString() {
        return String.format("\033[31mResponseException[code::%d msg::%s]\nposition-->%s", this.code, this.message, this.position);
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }


}
